package com.itheima.web.controller.system;

import com.itheima.common.utils.UtilFuns;
import com.itheima.doamin.system.Role;
import com.itheima.service.system.RoleService;
import com.itheima.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController{
    /**
     *fenye 查询所有
     */
    @Autowired
    RoleService roleService;
    @RequestMapping("list")
    public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "3") int size){
        String companyId="1";
        request.setAttribute("page",roleService.findAll(companyId,page,size));
        return "system/role/role-list";
    }

    //添加
    @RequestMapping("toAdd")
    public String toAdd(){
        List<Role> deptList = roleService.findAll(super.getCurrentUser().getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/role/role-add";
    }

    //更新
    @RequestMapping("toUpdate")
    public String toUpdate(String id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        List<Role> roleList = roleService.findAll(super.getCurrentUser().getCompanyId());
        request.setAttribute("roleList",roleList);
        return "system/role/role-update";
    }

    /**
     * 进行更新或则添加操作
     * @param role
     * @return
     */
    @RequestMapping("edit")
    public String edit(Role role){
        if (UtilFuns.isEmpty(role.getId())){
            //为空则保存
            role.setCompanyId(super.getCurrentUser().getCompanyId());
            role.setCompanyName(super.getCurrentUser().getCompanyName());
            roleService.save(role);
        }else {
            roleService.update(role);
        }
        return "redirect:/system/role/list";
    }

    //删除
    @RequestMapping("delete")
    public String delete(String id){
        roleService.delete(id);
        return "redirect:/system/role/list";
    }

    //权限页面
    @RequestMapping("roleModule")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "system/role/role-module";
    }

    /**
     * 返回角色权限数据
     */
    @RequestMapping("initModuleData")
    public @ResponseBody List<Map> initModuleData(String id){
        List<Map> roleModule = roleService.findRoleModule(id);
        return roleModule;
    }


    /**
     * 更新角色权限
     * @param roleid
     * @param moduleIds
     * @return
     */
    @RequestMapping("updateRoleModule")
    public String updateRoleModule(String roleid,String moduleIds){
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list";
    }
}
